package cm.iccsoft.android.telecomcost.adapter;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.CountryListActivity;
import cm.iccsoft.android.telecomcost.entities.Country;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CountryAdapter extends ArrayAdapter<Country> {
	private ArrayList<Country> items;
	private Context _context;
	private Activity _activity;

	public CountryAdapter(Context context, int textViewResourceId,
			ArrayList<Country> items, Activity activity) {
		super(context, textViewResourceId, items);
		this.items = items;
		_context = context;
		_activity = activity;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 * @param objects
	 */
	public CountryAdapter(Context context, int resource,
			int textViewResourceId, ArrayList<Country> objects) {
		super(context, resource, textViewResourceId, objects);
		this.items = objects;
		_context = context;
	}

	/**
	 * @param context
	 * @param resource
	 * @param textViewResourceId
	 */
	public CountryAdapter(Context context, int resource, int textViewResourceId) {
		super(context, resource, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public CountryAdapter(Context context, int textViewResourceId,
			List<Country> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 */
	public CountryAdapter(Context context, int textViewResourceId,
			Country[] objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param context
	 * @param textViewResourceId
	 */
	public CountryAdapter(Context context, int textViewResourceId) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			Log.i("Country_ListView", "creating new Country_row_view");
			LayoutInflater vi = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(
					cm.iccsoft.android.telecomcost.R.layout.country_view, null);
		} else {
			Log.i("Country_ListView", "using existing Country_row_view");
		}

		final Country country = items.get(position);
		if (country != null) {
			final CheckBox chk = (CheckBox) v
					.findViewById(cm.iccsoft.android.telecomcost.R.id.checkBox1);
			if (chk != null) {
				chk.setText(country.toString());
				chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					public void onCheckedChanged(CompoundButton arg0,
							boolean arg1) {
						selectCountry(country, arg1);
						chk.setChecked(country.isSelected());
					}
				});
			}

			final Button btnEditCountry = (Button) v
					.findViewById(cm.iccsoft.android.telecomcost.R.id.btnEditCountry);
			if (btnEditCountry != null) {
				btnEditCountry.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showEditDialog(country);
					}
				});
			}
		}
		return v;
	}

	protected void showEditDialog(Country country) {
		Bundle data = new Bundle();
		Gson gson = TelecomCost.getGson();
		data.putString(Country.PROP_TYPE, gson.toJson(country));
		_activity.showDialog(CountryListActivity.DIALOG_EDIT_COUNTRY, data);
	}

	protected void selectCountry(Country Country, boolean arg1) {
		Country.setSelected(arg1);

		if (arg1) {
			Log.i("Country_ListView", "Country " + Country.getName()
					+ " checked");
		} else {
			Log.i("Country_ListView", "Country " + Country.getName()
					+ " unchecked");
		}
	}
}
